#include<bits/stdc++.h>
#define int long long
using namespace std;
namespace IO
{
	template<typename T>
	void read(T &_x){_x=0;int _f=1;char ch=getchar();while(!isdigit(ch)) _f=(ch=='-'?-1:_f),ch=getchar();while(isdigit(ch)) _x=_x*10+(ch^48),ch=getchar();_x*=_f;}
	template<typename T,typename... Args>
	void read(T &_x,Args&...others){Read(_x);Read(others...);}
	const int BUF=20000000;char buf[BUF],top,stk[32];int plen;
	#define pc(x) buf[plen++]=x
	#define flush(); fwrite(buf,1,plen,stdout),plen=0;
	template<typename T>inline void print(T x){if(!x){pc(48);return;}if(x<0) x=-x,pc('-');for(;x;x/=10) stk[++top]=48+x%10;while(top) pc(stk[top--]);}
}
using namespace IO;
const int N = 1e6+10;
int n,m,a[N],b[N],c[N],lg[N],fa[N][24],head[N],cnt,head1[N],cnt1,dep[N],siz[N],dfn[N],xa[N],ans,R,R1;
struct w
{
	int to,nxt;
}t[N<<1],t1[N<<1];
inline void add(int x,int y)
{
	t[++cnt].nxt = head[x];
	t[cnt].to = y;
	head[x] = cnt;
}
inline void add1(int x,int y)
{
	t1[++cnt1].nxt = head1[x];
	t1[cnt1].to = y;
	head1[x] = cnt1;
}
void dfs(int x,int y)//n
{
	dfn[x] = ++cnt; xa[cnt] = x; siz[x] = 1;
	for(int i = head[x];i;i = t[i].nxt)
		if(t[i].to != y) dfs(t[i].to,x),siz[x] += siz[t[i].to];
}
void dfs1(int x,int y)//m
{
	dep[x] = dep[y]+1;
	fa[x][0] = y;
	for(int i = 1;i <= lg[dep[x]];i++) fa[x][i] = fa[fa[x][i-1]][i-1];
	for(int i = head1[x];i;i = t1[i].nxt)
		if(t1[i].to != y) dfs1(t[i].to,x);
}
int lca(int x,int y)
{
	if(dep[x] < dep[y]) swap(x,y);
	while(dep[x] != dep[y]) x = fa[x][lg[dep[x]-dep[y]]];
	if(x == y) return x; 
	for(int i = lg[dep[x]];i >= 0;i--)
		if(fa[x][i] != fa[y][i]) x = fa[x][i],y = fa[y][i];
	return fa[x][0];
}
signed main()
{
	freopen("tree.in","r",stdin);
	freopen("tree.out","w",stdout);
	read(n),read(m);
	for(int i = 1;i <= n;i++)
	{
		read(a[i]);
		if(a[i]) add(a[i],i);
		else R = i;
	}
	for(int i = 2;i <= m;i++) lg[i] = lg[i/2]+1;
	for(int i = 1;i <= m;i++) 
	{
		read(b[i]);
		if(b[i]) add1(b[i],i);
		else R1 = i;
	}
	for(int i = 1;i <= n;i++) read(c[i]);
	cnt = 0; dep[0] = -1; dfs(R,0),dfs1(R1,0);
	for(int i = 1;i <= n;i++)
		for(int j = i+1;j <= n;j++)
		{
			if(dfn[i] <= dfn[j] && dfn[j] <= dfn[i]+siz[i]-1);
			else if(dfn[j] <= dfn[i] && dfn[i] <= dfn[j]+siz[j]-1);
			else ans += (dep[lca(c[i],c[j])]+1)*siz[i]*siz[j];
		}
	for(int i = 1;i <= n;i++)
		for(int j = head[i];j;j = t[j].nxt)
			for(int z = dfn[t[j].to];z <= dfn[t[j].to]+siz[t[j].to]-1;z++)
				ans += (dep[lca(c[i],c[xa[z]])]+1)*(n-siz[t[j].to])*siz[xa[z]];
	print(ans); flush();
	return 0;
}
